import { Service } from 'egg';
// 角色关联菜单增删改查
export default class RoleMenuService extends Service {
  async getRoleMenuList() {
    const { ctx } = this;
    const roleMenuList = await ctx.model.RoleMenu.findAll({
      order: [[ 'sort', 'ASC' ]],
    });
    return roleMenuList;
  }
  async getRoleMenuListByRoleId(roleId) {
    const { ctx } = this;
    const roleMenuList = await ctx.model.RoleMenu.findAll({
      where: {
        roleId,
      },
      order: [[ 'sort', 'ASC' ]],
    });
    return roleMenuList;
  }
  async getRoleMenuListByMenuId(menuId) {
    const { ctx } = this;
    const roleMenuList = await ctx.model.RoleMenu.findAll({
      where: {
        menuId,
      },
      order: [[ 'sort', 'ASC' ]],
    });
    return roleMenuList;
  }
  async addRoleMenu(roleMenu) {
    const { ctx } = this;
    const roleMenuList = await ctx.model.RoleMenu.create(roleMenu);
    return roleMenuList;
  }
  async updateRoleMenu(roleMenu) {
    const { ctx } = this;
    const roleMenuList = await ctx.model.RoleMenu.update(roleMenu, {
      where: {
        id: roleMenu.id,
      },
    });
    return roleMenuList;
  }
  async getRoleMenuById(id) {
    const { ctx } = this;
    const roleMenuList = await ctx.model.RoleMenu.findOne({
      where: {
        id,
      },
    });
    return roleMenuList;
  }
}

